package com.webermagem.DAO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;

import com.webermagem.model.Sintoma;
import com.webermagem.util.BDUtil;
import com.webermagem.util.HibernateUtil;

public class SintomaDAO extends GenericDAO{
	
	public SintomaDAO(){
		
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Sintoma> listById(Session session, List<Long> listaId) {
		
		if (session == null){
			session = HibernateUtil.getSessionFactory().openSession();
		}
		
		Query query = session.createQuery(" from Sintoma where " + BDUtil.gerarInSintoma(listaId) + " order by id");

		List<Sintoma> objetoRetornado = query.list();
		
		session.flush();
		
		return objetoRetornado;
		
	}
	
	@SuppressWarnings("unchecked")
	public List<Sintoma> listByDescricao(Session session, String descricao) {
		
		if (session == null){
			session = HibernateUtil.getSessionFactory().openSession();
		}
		
		Query query = session.createQuery(" from Sintoma where lower(descricao) like :descricao order by id");
		query.setString("descricao", "%" + descricao.toLowerCase() + "%");
		List<Sintoma> objetoRetornado = query.list();
		session.flush();
		
		return objetoRetornado;
		
	}
	
	@SuppressWarnings("unchecked")
	public List<Sintoma> listOrdenado(Session session) {
		
		if (session == null){
			session = HibernateUtil.getSessionFactory().openSession();
		}
		
		Query query = session.createQuery(" from Sintoma order by id");
		List<Sintoma> objetoRetornado = query.list();
		session.flush();
		
		return objetoRetornado;
		
	}
	
	@SuppressWarnings("unchecked")
	public Map<String, Long> map(Session session) {
		
		if (session == null){
			session = HibernateUtil.getSessionFactory().openSession();
		}
		
		Query query = session.createQuery(" from Sintoma order by id");
		
		List<Sintoma> objetoRetornado = query.list();
		
		session.flush();
		
		Map<String, Long> map = new HashMap<String, Long>();
		
		for (Sintoma sintoma : objetoRetornado) {
			
			map.put(sintoma.getDescricao(), sintoma.getId());
			
		}
		
		return map;
		
	}

}
